package com.yunji.storage.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yunji.common.base.BaseDaoImpl;
import com.yunji.common.framework.MybatisPlusHelper;
import com.yunji.common.utils.bean.BeanUtils;
import com.yunji.storage.bo.ExistsStockBO;
import com.yunji.storage.bo.StockListQueryBO;
import com.yunji.storage.bo.StockSelectQueryBO;
import com.yunji.storage.dao.IStockDao;
import com.yunji.storage.domain.StockDO;
import com.yunji.storage.mapper.StockMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;


/**
 * @Project: yunji-coffee
 * @Package: com.yunji.storage.dao.impl
 * @Title: StockDaoImpl
 * <p>
 * History:
 * Date                     Version     Author          Summary
 * ============================================================
 * 2024-10-23 17:04:35      V1.0        HuaAo       新建类
 */
@Repository
public class StockDaoImpl extends BaseDaoImpl<StockMapper, StockDO> implements IStockDao {

    @Resource
    StockMapper stockMapper;

    @Override
    public StockDO selectStock(StockDO stock) {
        LambdaQueryWrapper<StockDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StockDO::getBelongId, stock.getBelongId()).eq(StockDO::getProductId, stock.getProductId());
        return stockMapper.selectOne(wrapper);
    }

    @Override
    public boolean create(StockDO stock) {
        return stockMapper.insert(stock) > 0;
    }

    @Override
    public boolean edit(StockDO stock) {
        LambdaUpdateWrapper<StockDO> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(StockDO::getStockId, stock.getStockId());
        return stockMapper.update(stock, wrapper) > 0;
    }

    @Override
    public boolean updateStock(StockDO stock) {
        return lambdaUpdate()
                .setSql(BeanUtils.isNotNull(stock.getAllStock()), MybatisPlusHelper.columnAddCount(StockDO::getAllStock, stock.getAllStock()))
                .setSql(BeanUtils.isNotNull(stock.getUsableStock()), MybatisPlusHelper.columnAddCount(StockDO::getUsableStock, stock.getUsableStock()))
                .setSql(BeanUtils.isNotNull(stock.getFrozenStock()), MybatisPlusHelper.columnAddCount(StockDO::getFrozenStock, stock.getFrozenStock()))
                .setSql(BeanUtils.isNotNull(stock.getDisableStock()), MybatisPlusHelper.columnAddCount(StockDO::getDisableStock, stock.getDisableStock()))
                .eq(StockDO::getProductId, stock.getProductId())
                .eq(StockDO::getBelongId, stock.getBelongId()).update();
    }

    @Override
    public boolean deleteEmptyStock(Long... ids) {
        int size = stockMapper.delete(new LambdaQueryWrapper<StockDO>()
                .in(StockDO::getStockId, Arrays.asList(ids))
                .eq(StockDO::getAllStock, BigDecimal.ZERO)
                .eq(StockDO::getUsableStock, BigDecimal.ZERO)
                .eq(StockDO::getFrozenStock, BigDecimal.ZERO)
                .eq(StockDO::getDisableStock, BigDecimal.ZERO));
        return ids.length == size ? Boolean.TRUE : Boolean.FALSE;
    }

    @Override
    public StockDO select(StockSelectQueryBO bo) {
        return getOne(new LambdaQueryWrapper<StockDO>()
                .eq(StockDO::getBelongId, bo.getBelongId())
                .eq(StockDO::getProductId, bo.getProductId()));
    }

    @Override
    public List<StockDO> search(StockListQueryBO bo) {
        return list(new LambdaQueryWrapper<StockDO>()
                .eq(BeanUtils.isNotNull(bo.getProductId()), StockDO::getProductId, bo.getProductId())
                .eq(BeanUtils.isNotNull(bo.getBelongId()), StockDO::getBelongId, bo.getBelongId()));
    }

    @Override
    public boolean existsStock(ExistsStockBO bo) {
        return lambdaQuery().in(BeanUtils.isNotNull(bo.getProductIds()),StockDO::getProductId,bo.getProductIds()).exists();
    }

}